package com.tinyseed.emcp.api.app.common;

import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by simon on 2018/11/7.
 */

public class DateUtil {

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private DateUtil() {
        // 防止外部创建实例
    }

    /**
     * 系统当前日期
     *
     * @return
     */
    public static final Date now() {
        return new Date();
    }

    public static Date parse(Object date) {
        if (date instanceof Date) {
            return (Date) date;
        }
        if (date == null) {
            return null;
        }
        String dateStr = date.toString();
        if (dateStr.length() == DATE_TIME_FORMAT.length()) {
            return parseDateTime(dateStr);
        } else if (dateStr.length() == DATE_FORMAT.length()) {
            return parseDate(dateStr);
        } else {
            throw new RuntimeException("parse error:" + dateStr);
        }

    }

    public static Date parseDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException("parseDate error:" + dateStr);
        }
    }

    public static Date parseDateTime(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException("parseDateTime error:" + dateStr);
        }
    }

    public static String formatDateTime(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            throw new RuntimeException("formatDateTime error");
        }
    }

    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            throw new RuntimeException("formatDate error");
        }
    }

    /*UTC时间转换类*/
    public static Date parseDateUTCTime(String dataStr) {

        DateTime dt = new DateTime(dataStr);
        return dt.toDate();
    }
    public static Date localDateTime2Date( LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        return Date.from(zdt.toInstant());
    }

    public static Timestamp parseTimeStamp(String s) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        String sd = m.replaceAll("");

        return new Timestamp(Long.valueOf(sd));
    }



}
